#
# Wii Makefile
#

.PHONY: package

ifeq ($(strip $(DEVKITPRO)),)
$(error "DEVKITPRO must be set in your environment.")
endif

ifeq ($(strip $(DEVKITPPC)),)
$(error "DEVKITPPC must be set in your environment.")
endif

AS      := powerpc-eabi-as
CC      := powerpc-eabi-gcc
CXX     := powerpc-eabi-g++
AR      := powerpc-eabi-ar
OBJCOPY := powerpc-eabi-objcopy
ELF2DOL := elf2dol

#
# Wii target rules
#
STRIP  = /bin/true

#
# Override library paths
#

VORBIS_CFLAGS  = -Iarch/wii/libvorbis/include -Iarch/wii/libogg/include
VORBIS_LDFLAGS = -Larch/wii/libvorbis/lib -Larch/wii/libogg/lib \
                 -lvorbisfile -lvorbis -logg

EXTRA_INCLUDES = -I$(DEVKITPRO)/libogc/include
EXTRA_LIBS     = -L$(DEVKITPRO)/libogc/lib/wii -lfat -lasnd -lwiikeyboard \
                 -lwiiuse -lbte -logc -lstdc++ -lm

MACHDEP = -DGEKKO -mrvl -mcpu=750 -meabi -mhard-float

ARCH_CFLAGS   += ${EXTRA_INCLUDES} $(MACHDEP)
ARCH_CXXFLAGS += ${ARCH_CFLAGS}
ARCH_LDFLAGS  += ${EXTRA_LIBS} $(MACHDEP)

package: mzx
	${ELF2DOL} ${mzxrun} boot.dol
	${ELF2DOL} ${mzx} megazeux.dol

clean:
	@rm -f boot.dol megazeux.dol arch/wii/*.d arch/wii/*.o

#
# Vile hack, remove me ASAP
#
arch/wii/%.o: arch/wii/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -c $< -o $@

#
# Need to nest Wii binaries in a subdir
#
build := build/${SUBPLATFORM}/apps/megazeux
build: package ${build}
	${CP} arch/wii/pad.config arch/wii/icon.png ${build}
	${CP} boot.dol megazeux.dol ${build}
	@sed "s/%VERSION%/${VERSION}/g;s/%DATE%/`date -u +%Y%m%d%H%M`/g" \
	    arch/wii/meta.xml > ${build}/meta.xml
	${RM} ${build}/${mzxrun} ${build}/${mzxrun}.debug
	${RM} ${build}/${mzx} ${build}/${mzx}.debug

include arch/zip.inc
